clearvars -except Globaloption option

%% Saving parameter estimates to automatically update the paper
fID = fopen('../../results/tables_main/UK/preWWI/TablePsi_preWWI_UK.tex', 'w');

%% Baseline Results
loaddata_4CIjoint_annual_cy_partial;

% Psi
Psichar = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'];
namebegin = '\\newcommand{\\preWWIPsi';
nameend1 = '}{%4.2f}\n';
nameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [namebegin Psichar(ii) Psichar(jj) nameend1], Psi(ii, jj));
    end

end

% maxeig and R2
fprintf(fID, '\\newcommand{\\preWWImaxeig}{%4.3f}\n', max(abs(eig(Psi))));
fprintf(fID, '\\newcommand{\\preWWIRsqra}{%4.1f}\n', 100 * R2(1));
fprintf(fID, '\\newcommand{\\preWWIRsqrb}{%4.1f}\n', 100 * R2(2));
fprintf(fID, '\\newcommand{\\preWWIRsqrc}{%4.1f}\n', 100 * R2(3));
fprintf(fID, '\\newcommand{\\preWWIRsqrd}{%4.1f}\n', 100 * R2(4));
fprintf(fID, '\\newcommand{\\preWWIRsqre}{%4.1f}\n', 100 * R2(5));
fprintf(fID, '\\newcommand{\\preWWIRsqrg}{%4.1f}\n', 100 * R2(6));
fprintf(fID, '\\newcommand{\\preWWIRsqrh}{%4.1f}\n', 100 * R2(7));
fprintf(fID, '\\newcommand{\\preWWIRsqrj}{%4.1f}\n', 100 * R2(9));

% T stat
Tstatchar = Psichar;
tnamebegin = '\\newcommand{\\preWWITstatt';
tnameend1 = '}{%4.2f}\n';
tnameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [tnamebegin Tstatchar(ii) Tstatchar(jj) tnameend1], tstat(ii, jj));
    end

end

% Sig
Sigchar = Psichar;
snamebegin = '\\newcommand{\\PreWWISig';
snameend1 = '}{%4.2f}\n';
snameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [snamebegin Sigchar(ii) Sigchar(jj) snameend1], 100 * Sig(ii, jj));
    end

end

% X2 mean
loaddata_4CIjoint_annual;

fprintf(fID, '\\newcommand{\\preWWIVarMeana}{%4.2f}\n', 100 * pi0);
fprintf(fID, '\\newcommand{\\preWWIVarMeanb}{%4.2f}\n', 100 * y0nom_1);
fprintf(fID, '\\newcommand{\\preWWIVarMeanc}{%4.2f}\n', 100 * yspr0);
fprintf(fID, '\\newcommand{\\preWWIVarMeand}{%4.2f}\n', 100 * x0);
fprintf(fID, '\\newcommand{\\preWWIVarMeane}{%4.2f}\n', 100 * mean(divgrm));
fprintf(fID, '\\newcommand{\\preWWIVarMeanf}{%4.2f}\n', mean(exp(pdm)));
fprintf(fID, '\\newcommand{\\preWWIVarMeanh}{%4.2f}\n', 100 * mean(deltalogtau));
fprintf(fID, '\\newcommand{\\preWWIVarMeani}{%4.2f}\n', 100 * mean((taxrevgdp)));
fprintf(fID, '\\newcommand{\\preWWIVarMeanj}{%4.2f}\n', 100 * mean(deltalogg));
fprintf(fID, '\\newcommand{\\preWWIVarMeank}{%4.2f}\n', 100 * mean((spendgdp)));

load MAT/UKprepara_30.mat
fprintf(fID, '\\newcommand{\\preWWIkzerox}{%4.2f}\n', k0x);
fprintf(fID, '\\newcommand{\\preWWIkonex}{%4.2f}\n', k1x);
fprintf(fID, '\\newcommand{\\preWWImeanpvsurplus}{%4.2f}\n', mean(s));
fprintf(fID, '\\newcommand{\\preWWImeanpvsurpluspct}{%4.2f}\n', 100 * mean(s));
fprintf(fID, '\\newcommand{\\preWWImeansurpluszeroz}{%4.2f}\n', mean(upper));
fprintf(fID, '\\newcommand{\\preWWImeansurpluszerozpct}{%4.2f}\n', 100 * mean(upper));
fprintf(fID, '\\newcommand{\\preWWIukpdgdp}{%4.2f}\n', mean(exp(pxbar)));

fprintf(fID, '\\newcommand{\\preWWIuksurplus}{%4.2f}\n', mean(surplusgdp));
fprintf(fID, '\\newcommand{\\preWWIuksurpluspct}{%4.2f}\n', 100 * mean(surplusgdp));
fprintf(fID, '\\newcommand{\\preWWIukdebtgdp}{%4.2f}\n', nanmean(gdebt));
fprintf(fID, '\\newcommand{\\preWWIukdebtgdppct}{%4.2f}\n', 100 * nanmean(gdebt));
fprintf(fID, '\\newcommand{\\preWWIukpvstodebt}{%4.2f}\n', 100 * nanmean(s) / nanmean(gdebt));
fprintf(fID, '\\newcommand{\\preWWIgdpreturn}{%4.2f}\n', 100 * mean(gdpreturn));

% 
load MAT/UKprepara_20.mat
fprintf(fID, '\\newcommand{\\upperUKpreWWItwenty}{%4.2f}\n', upper);
fprintf(fID, '\\newcommand{\\upperUKpreWWItwentypct}{%4.2f}\n', upper * 100);
fprintf(fID, '\\newcommand{\\smeanUKpreWWItwenty}{%4.2f}\n', mean(s));
fprintf(fID, '\\newcommand{\\smeanUKpreWWItwentypct}{%4.2f}\n', mean(s) * 100);
fprintf(fID, '\\newcommand{\\preWWIukpvstodebttwenty}{%4.2f}\n', 100 * nanmean(s) / nanmean(gdebt));
fprintf(fID, '\\newcommand{\\preWWIukcorrpvsdebttwenty}{%4.2f}\n', corr(gdebt(2:end), s'));


load MAT/UKprepara_30_cy.mat
fprintf(fID, '\\newcommand{\\cypreWWIukpdgdp}{%4.2f}\n', mean(exp(pxbar)));

%% Close File
fclose(fID); 